﻿using ET.DBCommon.Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET.DBCommon.DAL
{
    public class TestPaperInfoDAL
    {
        public static List<TestPaperInfoModel> GetUpdateExamInfo(int TestPaperId)
        {
            List<TestPaperInfoModel> testPaperInfoModels = new List<TestPaperInfoModel>();
            string sql = "select * from testpaperinfo where TestPaperId={0}";
            sql = string.Format(sql, TestPaperId);
            DataSet ds= DbHelper.ExecuteDataset(sql);
            DataTable dt = ds.Tables[0];
            if(dt!=null)
            {
                for(int i=0;i<dt.Rows.Count;i++)
                {
                    TestPaperInfoModel testPaperInfoModel = new TestPaperInfoModel();
                    testPaperInfoModel.Id = Convert.ToInt32(dt.Rows[i]["Id"].ToString());
                    testPaperInfoModel.TestPaperId = Convert.ToInt32(dt.Rows[i]["TestPaperId"].ToString());
                    testPaperInfoModel.OldOrderNumber = dt.Rows[i]["OldOrderNumber"].ToString() == "" ? 0 : Convert.ToInt32(dt.Rows[i]["OldOrderNumber"].ToString());
                    testPaperInfoModel.NewOrderNumber = dt.Rows[i]["NewOrderNumber"].ToString()==""?0:Convert.ToInt32(dt.Rows[i]["NewOrderNumber"].ToString());
                    testPaperInfoModel.TrainId = Convert.ToInt32(dt.Rows[i]["TrainId"].ToString());
                    testPaperInfoModel.Branch = dt.Rows[i]["Branch"].ToString();
                    testPaperInfoModels.Add(testPaperInfoModel);
                }
            }
            return testPaperInfoModels;
        }
        //插入数据 Add by gy
        public static void InsertExamInfo(int TestPaperID,int OldOrderNumber,int NewOrderNumber,int TrainId,string Branch)
        {
            string sql = "insert into testpaperinfo(TestPaperId,OldOrderNumber,NewOrderNumber,TrainId,Branch) value('" + TestPaperID + "','" + OldOrderNumber + "','" + NewOrderNumber + "','" + TrainId + "','" + Branch + "')";
            DbHelper.ExecuteNonQuery(sql);
        }
        //更新数据
        public static bool UpdateExamInfo(int Id,int TestPaperID, int OldOrderNumber, int NewOrderNumber, int TrainId, string Branch)
        {
            string sql = "Update testpaperinfo set OldOrderNumber=@OldOrderNumber,NewOrderNumber=@NewOrderNumber,TrainId=@TrainId,Branch=@Branch WHERE Id=@Id";
            MySqlParameter[] parms = {
                new MySqlParameter("@OldOrderNumber", OldOrderNumber),
                new MySqlParameter("@NewOrderNumber", NewOrderNumber),
                new MySqlParameter("@TrainId", TrainId),
                new MySqlParameter("@Branch", Branch),
                new MySqlParameter("@Id", Id)
            };
           return DbHelper.ExecuteNonQuery(sql,parms)>0;
        }
        //删除数据
        public static bool DeleteExamInfo(int Id)
        {
            string sql = "delete from testpaperinfo where Id =@Id";
            MySqlParameter[] parms = {
                new MySqlParameter("@Id", Id)
            };
           return DbHelper.ExecuteNonQuery(sql,parms)>0;
        }
    }
}
